package com.nny.alarm.more;

import java.util.Calendar;

import com.nny.alarm.R;
import com.uxwine.alarm.Alarm;
import com.uxwine.alarm.AlarmCalc;
import com.uxwine.alarm.TimeCycle2;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class AlarmDetailView
{
	Context mCtx;
	AlarmBriefUtil mAUtil;
	
	Alarm mAlarm;
	View mCtnMain;
	View mCtnDetail;
	View mCtnDel;
	View mCtnDis;
	TextView mtvTipDel;
	TextView mtvTipDis;
	TextView mtvClock;
	TextView mtvCycle;
	TextView mtvBrief;
	ImageView mivState;
	
	TypedArray mStatesResId;
	TypedArray mColorResId;
	int mnPosition;
	
	public AlarmDetailView(Context ctx, Alarm alarm, AlarmBriefUtil util)
	{
		mCtx = ctx;
		mAlarm = alarm;
		mAUtil = util;
	}
	
	public void setResIds(TypedArray states, TypedArray colors)
	{
		mStatesResId = states;
		mColorResId = colors;
	}
	
	public View createView(LayoutInflater in, int position, Typeface tf)
	{
		mnPosition = position;
		mCtnMain = in.inflate(R.layout.listitem_alarm, null);
		mCtnDel = mCtnMain.findViewById(R.id.li_alarm_ctn_delete);
		mCtnDetail = mCtnMain.findViewById(R.id.li_alarm_ctn_detail);
		mCtnDis = mCtnMain.findViewById(R.id.li_alarm_ctn_disable);
		
		mtvTipDel = (TextView)mCtnMain.findViewById(R.id.li_alarm_tv_del);
		mtvTipDis = (TextView)mCtnMain.findViewById(R.id.li_alarm_tv_dis);
		mtvClock = (TextView)mCtnMain.findViewById(R.id.li_alarm_tv_clock);
		mtvCycle = (TextView)mCtnMain.findViewById(R.id.li_alarm_tv_recycle);
		mtvBrief = (TextView)mCtnMain.findViewById(R.id.li_alarm_tv_brief);
		mivState = (ImageView)mCtnMain.findViewById(R.id.li_alarm_iv_state);
		

		mtvBrief.setTypeface(tf);
		mtvClock.setTypeface(tf);
		mtvCycle.setTypeface(tf);
		
		updateView();
		
		mCtnMain.setTag(this);		
		return mCtnMain;
	}
	
	
	public void updateView()
	{
		
		Calendar trigger = calcTrigger(mAlarm);
		
		mtvBrief.setText(mAlarm.label);		
		mtvClock.setText(mAUtil.getBaseTimeString(mAlarm, trigger));		
		mtvCycle.setText(mAUtil.getCycleString(mAlarm));
		
		if(null == mStatesResId || mColorResId == null)
			return;
		
		int index = mAlarm.enabled ?  mnPosition % 3 : 3;
		mCtnDetail.setBackgroundDrawable(mColorResId.getDrawable(index));
		mivState.setImageDrawable(mStatesResId.getDrawable(index));
	}
	
	public void showDelete(boolean will)
	{
		mCtnDel.setVisibility(View.VISIBLE);
		mCtnDetail.setVisibility(View.GONE);
		mCtnDis.setVisibility(View.GONE);
		
		if(will)
		{
			mtvTipDel.setText(R.string.edit_gesture_toright_delete);
//			mCtnDel.setBackgroundColor(Color.GRAY);
			mtvTipDel.setTextColor(Color.GRAY);
		}
		else
		{
			mtvTipDel.setText(R.string.edit_gesture_right_delete);
//			mCtnDel.setBackgroundColor(Color.RED);
			mtvTipDel.setTextColor(Color.RED);
		}		
	}
	
	public void showDisable(boolean will)
	{
		mCtnDel.setVisibility(View.GONE);
		mCtnDetail.setVisibility(View.GONE);
		mCtnDis.setVisibility(View.VISIBLE);
		
		if(!canEabled())
		{
			mtvTipDis.setText(mCtx.getString(R.string.edit_alarm_expired));
//			mtvTipDis.setBackgroundColor(Color.TRANSPARENT);
			//mCtnDis.setBackgroundColor(Color.GRAY);
			mtvTipDis.setTextColor(Color.GRAY);
			return;
		}
		
		if(will)
		{
			int id = mAlarm.enabled ? R.string.edit_gesture_toleft_disable : R.string.edit_gesture_toleft_enable;
			mtvTipDis.setText(id);
			mtvTipDis.setTextColor(Color.GRAY);
			//mCtnDis.setBackgroundColor(Color.GRAY);
//			mtvTipDis.setBackgroundColor(Color.TRANSPARENT);
		}
		else
		{
			int id = mAlarm.enabled ? R.string.edit_gesture_left_disable : R.string.edit_gesture_left_enable;
			mtvTipDis.setText(id);
			mtvTipDis.setTextColor(0xFFFF8800);
			//mCtnDis.setBackgroundColor(Color.YELLOW);
		}		
	}
	
	public void showDetail()
	{
		mCtnDel.setVisibility(View.GONE);
		mCtnDetail.setVisibility(View.VISIBLE);
		mCtnDis.setVisibility(View.GONE);	
	}
	

	
	public boolean canEabled()
	{
		Calendar trigger = calcTrigger(mAlarm);
		long now = System.currentTimeMillis();
		long time = trigger.getTimeInMillis();
		return (now < time + 1000);
	}
	
	public Calendar calcTrigger(final Alarm alarm)
	{
		Calendar trigger = AlarmCalc.triggerTime(alarm.time, alarm.cycle);
		if(alarm.cycle.getCycle() == TimeCycle2.TYPE_ONCE)
		{
			trigger.setTimeInMillis(alarm.trigger);
		}
		return trigger;
	}
}
